package com.annotation.lifeCycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 *   实现BeanPostProcessor的类常用的都有哪些
 *      InitDestroyAnnotationBeanPostProcessor：init和destroy方法的后处理，包括@PostConstruct和@PreDestroy注解
 *      AutowiredAnnotationBeanPostProcessor：处理含有@Autowired注解的参数注入
 *      ApplicationContextAwareProcessor：处理postProcessBeforeInitialization方法
 *      AsyncAnnotationBeanPostProcessor：处理@Async注解
 *      BeanValidationPostProcessor：参数校验
 *
 */

@Component
public class MyBeanPostProcessor implements BeanPostProcessor{

    private ApplicationContext applicationContext;

    /**
     * populateBean（实例化bean并为其赋值）
     * {
     *     applyBeanPostProcessorsBeforeInitialization
     *     invokeInitMethods（反射调用初始化方法）
     *     applyBeanPostProcessorsAfterInitialization
     * }
     */
    //bean是当前组建，beanName是当前组建在容器中的id
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization->" + bean + "->" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization->" + bean + "->" + beanName);
        return bean;
    }

}
